package com.jiukuaidao.zhulin.module.user.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.api.ApiUpdatePwd;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.user.contract.UpdatePasswordContract;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/27
 * Description :
 */
public class UpdatePasswordPresenter extends BasePresenter<UpdatePasswordContract.View, TasksRepository> implements UpdatePasswordContract.Presenter {
    public UpdatePasswordPresenter(TasksRepository repository, UpdatePasswordContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        updatePwd();
    }

    @Override
    public void updatePwd() {
        if (StringUtils.isEmpty(mView.veriCode())) {
            mView.toast(AppContext.getContext().getString(R.string.code_empty_hint));
            return;
        }
        if (StringUtils.isEmpty(mView.password())) {
            mView.toast(AppContext.getContext().getString(R.string.password_empty));
            return;
        }
        if (StringUtils.isEmpty(mView.rePassword())) {
            mView.toast(AppContext.getContext().getString(R.string.password_again_empty));
            return;
        }
        if (!mView.password().equals(mView.rePassword())) {
            mView.toast(AppContext.getContext().getString(R.string.password_different));
            return;
        }
        mRepository.loadData(new ApiUpdatePwd(mView.veriCode(), mView.password(), mView.rePassword(), new MyRetroCallBack() {
            @Override
            public void onSuccess(Object result, String errorCode, String msg) {
                mView.toast(AppContext.getContext().getString(R.string.save_success));
                mView.setFinish();
            }

            @Override
            public void onFail(Object result, String errorCode, String msg) {
                mView.toast(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(resCode + "");
            }
        }));
    }
}
